<?php
namespace app\api\controller;

use Gregwar\Image\Image;
use think\Controller;
use think\Request;
use think\Session;
use OSS\OssClient;
use OSS\Core\OssException;

/**
 * 通用上传接口
 * Class Upload
 * @package app\api\controller
 */
class Fileuploadfixdata extends Controller
{
    protected function _initialize()
    {

        parent::_initialize();
        if (!Session::has('admin_id')) {
            $result = [
                'error' => 1,
                'message' => '未登录'
            ];

            return json($result);
        }
    }

    /**
     * 实例化阿里云oos
     * @return object 实例化得到的对象
     */
    function new_oss()
    {
        vendor('Alioss.autoload');
        $config = config("ALIOSS_CONFIG");
        $oss    = new OssClient($config['KEY_ID'], $config['KEY_SECRET'], $config['END_POINT']);
        return $oss;
    }

    /**
     * 上传文件到oss并删除本地文件
     * @param  string $path 文件路径
     * @return bollear      是否上传
     */
    function oss_upload($path)
    {
        // 获取配置项
        $bucket = config('ALIOSS_CONFIG.BUCKET');
        // 先统一去除左侧的.或者/ 再添加./
        $oss_path = ltrim($path, './');
        $path     = $oss_path;

        if (file_exists($path)) {
            // 实例化oss类
            $oss = $this->new_oss();
            // 上传到oss
            $oss1 = $oss->uploadFile($bucket, $oss_path, $path);
            // 如需上传到oss后 自动删除本地的文件 则删除下面的注释
            return $oss1;
        }
        return false;
    }


    /**
     * 通用上传接口
     * @return \think\response\Json
     */
    public function upload(Request $request)
    {
        $date         = date('Y-m-d-H-i', time());//时间
        $file_key = 'images';
        $ali_new_name = pathinfo($request->param('path'))['basename'];
            try {
                // 获取配置项
                $bucket = config('ALIOSS_CONFIG.BUCKET');
                // 实例化oss类
                $oss  = $this->new_oss();
                $path = $request->param('path');
                // 上传到oss
                $oss1 = $oss->uploadFile($bucket, 'uploads/' . $date . '/' . $file_key . '/' . $ali_new_name, $path);
                if ($oss1) {
                    $result = [
                        'code' => 0,
                        'msg' => "success",
                        'data' => [
                            "src" => $oss1['info']['url'],
                            "title" => ''
                        ]
                    ];

                } else {
                    $result = [
                        'code' => 1,
                        'msg' => "error",
                        'data' => [
                            "src" => '',
                            "title" => ''
                        ]
                    ];
                }
                return json($result);
            } catch (OssException $e) {
                $result = [
                    'code' => 1,
                    'msg' => $e->getMessage(),
                    'data' => [
                        "src" => '',
                        "title" => ''
                    ]
                ];
                return json($result);
            }
        }






    private function check_dir($dirname = '')
    {
        if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
            return ['code' => 'ERROR', 'info' => '文件创建失败'];
        } else {
            if (!is_writeable($dirname)) {
                return ['code' => 'ERROR', 'info' => '文件不可写'];
            } else {
                return ['code' => 'SUCCESS', 'info' => 'SUCCESS_DIR'];
            }
        }
    }

    /**
     * 删除oss上指定文件
     * @param  string $object 文件路径 例如删除 /Public/README.md文件  传Public/README.md 即可
     */
    function oss_delet_object($object)
    {
        // 实例化oss类
        $oss = $this->new_oss();
        // 获取bucket名称
        $bucket = config('ALIOSS_CONFIG.BUCKET');
        $test   = $oss->deleteObject($bucket, $object);
    }
}